package com.nt.oam.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nt.common.core.domain.entity.SysRole;
import com.nt.common.core.domain.entity.SysUser;
import com.nt.common.enums.YesOrNoEnum;
import com.nt.common.utils.DateUtils;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;

import com.nt.common.utils.SecurityUtils;
import com.nt.common.utils.StringUtils;
import com.nt.common.utils.bean.BeanUtils;
import com.nt.oam.domain.AntDatabaseAsset;
import com.nt.oam.domain.AntHospital;
import com.nt.oam.domain.AntHospitalProject;
import com.nt.oam.vo.server.ServerMessageVo;
import com.nt.system.service.impl.SysRoleServiceImpl;
import com.nt.system.service.impl.SysUserServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.nt.oam.mapper.AntServerMapper;
import com.nt.oam.domain.AntServer;
import com.nt.oam.service.IAntServerService;

/**
 * 服务器信息Service业务层处理
 *
 * @author JZFeng
 * @date 2025-02-05
 */
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class AntServerServiceImpl extends ServiceImpl<AntServerMapper, AntServer> implements IAntServerService {
    private final AntServerMapper antServerMapper;
    private final SysRoleServiceImpl sysRoleService;
    private final AntHospitalServiceImpl antHospitalService;
    private final AntHospitalProjectServiceImpl antHospitalProjectService;

    private final SysUserServiceImpl sysUserService;

    /**
     * 查询服务器信息
     *
     * @param serverId 服务器信息主键
     * @return 服务器信息
     */
    @Override
    public AntServer selectAntServerByServerId(Long serverId) {
        return antServerMapper.selectAntServerByServerId(serverId);
    }

    /**
     * 查询服务器信息列表
     *
     * @param antServer 服务器信息
     * @return 服务器信息
     */
    @Override
    public List<AntServer> selectAntServerList(AntServer antServer) {
        return antServerMapper.selectAntServerList(antServer);
    }

    /**
     * 新增服务器信息
     *
     * @param antServer 服务器信息
     * @return 结果
     */
    @Override
    public int insertAntServer(AntServer antServer) {
        System.out.println("VPN : " + antServer.getVPN());
        antServer.setCreateTime(DateUtils.getNowDate());
        // 医院的id
        Long hospitalId = antServer.getHospitalId();
        antServer.setHospitalId(hospitalId);
        // 查询医院的名称
        AntHospital antHospital = antHospitalService.getById(hospitalId);
        if (Objects.nonNull(antHospital) && StringUtils.isNotEmpty(antHospital.getHospitalName())) {
            antServer.setHospitalName(antHospital.getHospitalName());
        }
        // 项目id
        Long projectId = antServer.getProjectId();
        antServer.setProjectId(projectId);
        AntHospitalProject antHospitalProject = antHospitalProjectService.getById(projectId);
        if (Objects.nonNull(antHospitalProject) && StringUtils.isNotEmpty(antHospitalProject.getProjectName())) {
            antServer.setProjectName(antHospitalProject.getProjectName());
        }
        // 创建人
        Long userId = SecurityUtils.getUserId();
        SysUser sysUser = sysUserService.selectUserById(userId);
        if (Objects.nonNull(sysUser) && StringUtils.isNotEmpty(sysUser.getUserName())) {
            antServer.setCreatePerson(sysUser.getUserName());
        }
        antServer.setIsDeleted(YesOrNoEnum.NO.getCode());
        antServer.setIsModify(YesOrNoEnum.NO.getCode());
        return antServerMapper.insertAntServer(antServer);
    }

    /**
     * 修改服务器信息
     *
     * @param antServer 服务器信息
     * @return 结果
     */
    @Override
    public int updateAntServer(AntServer antServer) {
        antServer.setUpdateTime(DateUtils.getNowDate());
        // 医院的id
        Long hospitalId = antServer.getHospitalId();
        antServer.setHospitalId(hospitalId);
        // 查询医院的名称
        AntHospital antHospital = antHospitalService.getById(hospitalId);
        if (Objects.nonNull(antHospital) && StringUtils.isNotEmpty(antHospital.getHospitalName())) {
            antServer.setHospitalName(antHospital.getHospitalName());
        }
        // 项目id
        Long projectId = antServer.getProjectId();
        antServer.setProjectId(projectId);
        AntHospitalProject antHospitalProject = antHospitalProjectService.getById(projectId);
        if (Objects.nonNull(antHospitalProject) && StringUtils.isNotEmpty(antHospitalProject.getProjectName())) {
            antServer.setProjectName(antHospitalProject.getProjectName());
        }
        // 创建人
        // 创建人
        Long userId = SecurityUtils.getUserId();
        SysUser sysUser = sysUserService.selectUserById(userId);
        if (Objects.nonNull(sysUser) && StringUtils.isNotEmpty(sysUser.getUserName())) {
            antServer.setUpdatePerson(sysUser.getUserName());
        }
        return antServerMapper.updateAntServer(antServer);
    }

    /**
     * 批量删除服务器信息
     *
     * @param serverIds 需要删除的服务器信息主键
     * @return 结果
     */
    @Override
    public int deleteAntServerByServerIds(Long[] serverIds) {
        List<Long> serverIdsList = Arrays.asList(serverIds);
        serverIdsList.forEach(a -> {
            AntServer antServer = antServerMapper.selectAntServerByServerId(a);
            antServer.setIsDeleted(YesOrNoEnum.YES.getCode());
            antServerMapper.updateAntServer(antServer);
        });
        // antServerMapper.deleteAntServerByServerIds(serverIds);
        return YesOrNoEnum.YES.getCode();
    }

    /**
     * 删除服务器信息信息
     *
     * @param serverId 服务器信息主键
     * @return 结果
     */
    @Override
    public int deleteAntServerByServerId(Long serverId) {
        return antServerMapper.deleteAntServerByServerId(serverId);
    }

    /**
     * 查询服务器信息详情页
     *
     * @param serverId 服务器信息主键
     * @return 服务器信息
     */
    @Override
    public ServerMessageVo selectServerMessage(Long serverId) {
        // 源对象
        AntServer antServer = antServerMapper.selectAntServerByServerId(serverId);
        //医院名称
        AntHospital antHospital = antHospitalService.selectAntHospitalByHospitalId(antServer.getHospitalId());
        if (Objects.nonNull(antHospital) && StringUtils.isNotEmpty(antHospital.getHospitalName())) {
            antServer.setHospitalName(antHospital.getHospitalName());
        }
        // 项目名称
        AntHospitalProject antHospitalProject = antHospitalProjectService.selectAntHospitalProjectByProjectId(antServer.getProjectId());
        if (Objects.nonNull(antHospitalProject) && StringUtils.isNotEmpty(antHospitalProject.getProjectName())) {
            antServer.setProjectName(antHospitalProject.getProjectName());
        }
        // 目标赋值对象
        ServerMessageVo serverMessageVo = new ServerMessageVo();
        BeanUtils.copyProperties(antServer, serverMessageVo);
        return serverMessageVo;
    }

    /**
     * 通过项目id来查询服务器信息
     *
     * @return 服务器信息
     */
    @Override
    public ServerMessageVo selectByProjectIdServerMessage(Long projectId) {
        // 源对象
        ServerMessageVo serverMessageVo = new ServerMessageVo();
        AntServer antServer = antServerMapper.selectByProjectIdServerMessage(projectId);
        if (Objects.nonNull(antServer)) {
            //医院名称
            AntHospital antHospital = antHospitalService.selectAntHospitalByHospitalId(antServer.getHospitalId());
            if (Objects.nonNull(antHospital) && StringUtils.isNotEmpty(antHospital.getHospitalName())) {
                antServer.setHospitalName(antHospital.getHospitalName());
            }
            // 项目名称
            AntHospitalProject antHospitalProject = antHospitalProjectService.selectAntHospitalProjectByProjectId(antServer.getProjectId());
            if (Objects.nonNull(antHospitalProject) && StringUtils.isNotEmpty(antHospitalProject.getProjectName())) {
                antServer.setProjectName(antHospitalProject.getProjectName());
            }
            // 目标赋值对象
            BeanUtils.copyProperties(antServer, serverMessageVo);
        }
        return serverMessageVo;
    }
    /**
     * 通过医院id来查询服务器信息
     *
     * @return 服务器信息
     */
    @Override
    public ServerMessageVo selectByHospitalIdServerMessage(Long hospitalId){
        // 源对象
        ServerMessageVo serverMessageVo = new ServerMessageVo();
        AntServer antServer = antServerMapper.selectByHospitalIdServerMessage(hospitalId);
        if (Objects.nonNull(antServer)) {
            //医院名称
            AntHospital antHospital = antHospitalService.selectAntHospitalByHospitalId(antServer.getHospitalId());
            if (Objects.nonNull(antHospital) && StringUtils.isNotEmpty(antHospital.getHospitalName())) {
                antServer.setHospitalName(antHospital.getHospitalName());
            }
            // 项目名称
            AntHospitalProject antHospitalProject = antHospitalProjectService.selectAntHospitalProjectByProjectId(antServer.getProjectId());
            if (Objects.nonNull(antHospitalProject) && StringUtils.isNotEmpty(antHospitalProject.getProjectName())) {
                antServer.setProjectName(antHospitalProject.getProjectName());
            }
            // 目标赋值对象
            BeanUtils.copyProperties(antServer, serverMessageVo);
        }
        return serverMessageVo;

    }

    /**
     * 查询是否在修改
     */
    public int selectServerModify(Long serverId) {
        AntServer antServer = this.getById(serverId);
        int isModify = 0;
        if (Objects.nonNull(antServer) && Objects.nonNull(antServer.getIsModify())) {
            isModify = antServer.getIsModify();
        }
        return isModify;
    }

    /**
     * 重新设置修改状态
     */
    public int updateServerModify(Long serverId, Integer isModify) {
        AntServer antServer = this.getById(serverId);
        if (Objects.nonNull(antServer)) {
            antServer.setIsModify(isModify);
        }
        return antServerMapper.updateAntServer(antServer);
    }

}
